layui.define(['form','echarts'], function(exports){
  var $ = layui.$,form = layui.form,echarts = layui.echarts;

  var tradeNo = $('input[name="tradeNo"]').val();
  var createDate = $('input[name="createDate"]').val();

    function initialChartData(){
        var loadIndex = layer.msg('处理中...', {shade: [0.8, '#393D49'], time: 60 * 60 * 1000});
        $.ajax({
            async: false,
            url: layui.setter.webRoot + '/cdcz/order/slot/instant_data/chart.data',
            data: {tradeNo:tradeNo,createDate:createDate},
            success: function (res) {
                layer.close(loadIndex);
                if (res.code == 200) {
                    console.log(res);
                    setPowerChartData(res.body.times, res.body.powerData);
                    setCurrentChartData(res.body.times, res.body.currentData);
                }
            }
        });
    }

    function setPowerChartData(times, data) {
        var powerPanel = document.getElementById('power-panel');
        var powerPanelChart = echarts.init(powerPanel);
        var option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            xAxis: {
                type: 'category',
                data: times,
                axisTick: {
                    alignWithLabel: true
                }
            },
            yAxis: {
                type: 'value',
                scale: true,
                //max: data,
                min: 0,
                splitNumber: 5,
                boundaryGap: [0.2, 0.2]
            },
            series : [{
                name:'Power',
                type:'line',
                smooth:true,
                itemStyle: {normal: {areaStyle: {type: 'default'}}},
                data: data
            }]
        };
        powerPanelChart.setOption(option);
    }

    function setCurrentChartData(times, data) {
        var currentPanel = document.getElementById('current-panel');
        var currentPanelChart = echarts.init(currentPanel);
        var option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            xAxis: {
                type: 'category',
                data: times,
                axisTick: {
                    alignWithLabel: true
                }
            },
            yAxis: {
                type: 'value',
                scale: true,
                //max: data,
                min: 0,
                splitNumber: 5,
                boundaryGap: [0.2, 0.2]
            },
            series : [{
                name:'Current',
                type:'line',
                smooth:true,
                itemStyle: {normal: {areaStyle: {type: 'default'}}},
                data: data
            }]
        };
        currentPanelChart.setOption(option);
    }

    //初始化图表
    initialChartData();

    //事件
    var active = {
        synMemberOrder:function (){
            layer.confirm('确定要同步会员订单吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/cdcz/order/member/syn.do',
                    data:{tradeNo:tradeNo,createDate:createDate},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            layer.msg('同步成功',{icon:1});
                            table.reload('LAY-table');
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    };

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
  
  exports('charge_socket_order_info', {})
});